package com.hwl.demo2;

import com.hwl.demo1.service.CashSuper;
import com.hwl.demo1.service.impl.CashNormal;
import com.hwl.demo1.service.impl.CashRebate;
import com.hwl.demo1.service.impl.CashReturn;

/**
 * 策略模式
 * 与简单工厂的结合
 *
 * @author sentry
 * @since 2023-02-25
 */
public class CashContext {

    private final CashSuper cs;

    public CashContext(int cashType) {
        switch (cashType) {
            case 2:
                this.cs = new CashRebate(0.8d);
                break;
            case 3:
                this.cs = new CashRebate(0.5d);
                break;
            case 4:
                this.cs = new CashReturn(300d, 100d);
                break;
            default:
                this.cs = new CashNormal();
        }
    }

    public double getResult(double price, int num) {
        // 根据收费策略的不同得到不同计算结果
        return this.cs.acceptCash(price, num);
    }

    public static void main(String[] args) {
        CashContext cc = new CashContext(2);
        double result = cc.getResult(15, 2);
        System.out.println("价格为：" + result);
    }
}
